package com.loan.mappers;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.loan.entity.Mortgage;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

/**
 * 资产mapper
 */
public interface MortgageMapper extends BaseMapper<Mortgage> {
    /**
     * 查询用户的资产
     * @return
     */
    @Select("SELECT * FROM mortgage WHERE cus_id = #{cusId}")
    @Results(id = "baseMap",value = {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "categoryId",column = "category_id"),
            @Result(property = "image",column = "image"),
            @Result(property = "appId",column = "app_id"),
            @Result(property = "companyId",column = "company_id"),
            @Result(property = "company",
                    column = "company_id",
                    one=@One(select = "com.loan.mappers.CompanyMapper.selectById")),
            @Result(property = "valuation",column = "valuation"),
            @Result(property = "time",column = "time"),
            @Result(property = "cusId",column = "cus_id"),
            @Result(property = "customer",
                    column = "cus_id",
                    one = @One(select = "com.loan.mappers.CustomerMapper.selectById")),

    })
    List<Mortgage> findByCusId(Integer cusId);

    @Select("SELECT * FROM mortgage WHERE app_id = #{appId}")
    @ResultMap("baseMap")
    List<Mortgage> findByAppId(Integer appId);

    /**
     * 条件查询
     * @param mortgage
     * @return
     */
    @SelectProvider(type = SqlProvider.class, method = "selectByCondition")
    @ResultMap("baseMap")
    List<Mortgage> condition(Mortgage mortgage);

    public static class SqlProvider {
        public static String selectByCondition(Mortgage mortgage) {
            SQL sql = new SQL();
            sql.SELECT("*").FROM("mortgage");
            if (mortgage.getId()!=null){
                sql.WHERE("id=#{id}");
            }
            if (mortgage.getName()!=null){
                sql.WHERE("name like concat('%',#{name},'%')");
            }
            if (mortgage.getCategoryId()!=null){
                sql.WHERE("category_id = #{categoryId}");
            }
            if (mortgage.getAppId()!=null){
                sql.WHERE("app_id = #{appId}");
            }
            if (mortgage.getCompanyId()!=null){
                sql.WHERE("company_id = #{companyId}");
            }
            if (mortgage.getCusId()!=null){
                sql.WHERE("cus_id = #{cusId}");
            }

            return sql.toString();
        }
    }
}
